{extend name="adminblue/base" /}
{block name="resources"/}
<style type="text/css">
.input-error{font-size: 12px;color: #74828D;line-height: 30px;display: none;}
.s{color: red;font-size: 18px;line-height: 24px;}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th width="20%" style="text-align: left;"><button class="btn-common" onclick="addExpressAddressDialog(0)" style="margin:0 !important;">添加物流</button></th>
		<th width="10%">
			<input type="text" id ='search_text' placeholder="请输入公司名称" class="input-common" />
			<input type="button" onclick="searchData()" value="搜索" class="btn-common" />
		</th>
	</tr>
</table>
<table class="table-class">
	<colgroup>
		<col style="width: 5%">
		<col style="width: 10%;">
		<col style="width: 10%;">
		<col style="width: 10%;">
		<col style="width: 15%;">
		<col style="width: 15%;">
		<col style="width: 10%;">
		<col style="width: 15%;">
		<col style="width: 10%;">
		<col style="width: auto;">
	</colgroup>
	<thead>
		<tr align="center">
			<th><input type="checkbox" onClick="CheckAll(event)"></th>
			<th>发货地址</th>
			<th>收货地址</th>
			<th>联系人</th>
			<th>所在地</th>
			<th>地址</th>
			<th>电话</th>
			<th>公司名称</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>

<!--添加发货地址-->
<div id="addExpressAddressDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3>添加发货地址</h3>
	</div>
	<div class="modal-body">
		<div class="form-horizontal">
			
			<div class="control-group">
				<label class="control-label" for="contact"><span class="color-red">*</span>联系人</label>
				<div class="controls">
					<input type="text" id="contact" placeholder="请输入原密码" class="input-common" />
					<span class="input-error" id='contactPrompt'><span class="s">×</span></span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="selProvince"><span class="color-red">*</span>所在地</label>
				<div class="controls">
					<select id="selProvince" class="input-small select-common" name="selProvince"></select>
					<select class="input-small select-common" id="selCity" name="selCity"></select>
					<select class="input-small select-common" id="selArea" name="selArea"></select>
					<span class="input-error" id='pcaPrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="address"><span class="color-red">*</span>详细地址</label>
				<div class="controls">
					<input name="address" id="address" type="text" class="input-common">
					<span class="input-error" id='addressPrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="zipcode"><span class="color-red">*</span>邮政编码</label>
				<div class="controls">
					<input name="zipcode" id="zipcode" type="text" class="input-common">
					<span class="input-error" id='zipcodePrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="phone"><span class="color-red">*</span>固定电话</label>
				<div class="controls">
					<input name="phone" id="phone" type="text" class="input-common">
					<span class="input-error" id='phonePrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="mobile"><span class="color-red">*</span>手机</label>
				<div class="controls">
					<input name="mobile" id="mobile" type="text" class="input-common">
					<span class="input-error" id='mobilePrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label" for="company_name"><span class="color-red">*</span>公司名称</label>
				<div class="controls">
					<input name="company_name" id="company_name" type="text" class="input-common">
					<span class="input-error" id='company_namePrompt'><span class="s">×</span>不可为空</span>
				</div>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button class="btn-common" id="addBtn" onClick="addExpressAddress()">保存</button>
		<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
	</div>
</div>
<input type="hidden" name="cityid" id="cityid" value="0" />
<input type="hidden" name="districtid" id="districtid" value="0" />
<input type="hidden" name="express_address_id" id="express_address_id" />
{/block}
{block name="script"}
<script type="text/javascript">
function LoadingInfo(pageIndex) {
	var search_text = $("#search_text").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/express/expressaddress')}",
		data : { "pageIndex" : pageIndex, "search_text" : search_text },
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
					html += '<td><input name="sub" type="checkbox" value="'+ data['data'][i]['express_address_id']+'" ></td>';
					
					/* 发货地址 */
					if(data["data"][i]["is_consigner"]==1){
						html += '<td><label for="consigner'+i+'"><input type="radio" name="consigner" id="consigner'+i+'" checked="checked" >默认</label></td>';
					}else{
						html += '<td><label for="consigner'+i+'"><input type="radio" name="consigner" id="consigner'+i+'" onclick="DefaultAddress(0,'+data["data"][i]["express_address_id"]+')">默认</label></td>';
					}
					/* 收货地址 */
					if(data["data"][i]["is_receiver"]==1){
						html += '<td><label for="receiver'+i+'"><input type="radio" name="receiver" id="receiver'+i+'" checked="checked"  >默认</label></td>';
					}else{
						html += '<td><label for="receiver'+i+'"><input type="radio" name="receiver" id="receiver'+i+'" onclick="DefaultAddress(1,'+data["data"][i]["express_address_id"]+')">默认</label></td>';
					}
					
					html += '<td>' + data["data"][i]["contact"] + '</td>';
					html += '<td>' + data["data"][i]["address_info"] + '</td>';
					html += '<td>' + data["data"][i]["address"] + '</td>';
					html += '<td>' + data["data"][i]["mobile"] + '</td>';
					html += '<td>' + data["data"][i]["company_name"] + '</td>';
					html += '<td><a href="javascript:addExpressAddressDialog('+data["data"][i]["express_address_id"]+');">修改|</a><a style="cursor: pointer;" onclick="DelExpressCompany('+data["data"][i]["express_address_id"]+')">删除</a></td> ';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="9">暂无符合条件的数据记录</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

//全选
function CheckAll(event){
	var checked = event.checked;
	$(".table-class tbody input[type = 'checkbox']").prop("checked",checked);
}

function searchData(){
	LoadingInfo(1);
}

//物流地址删除
function DelExpressCompany(express_address_id){

	$( "#dialog" ).dialog({
		buttons: {
			"确定,#0059d6,#fff": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/express/deleteshopexpressaddress')}",
					data : { "express_address_id" : express_address_id },
					success : function(data) {
						if (data["code"] > 0) {
							showMessage('success', data["message"]);
							LoadingInfo(1);
						}else{
							showMessage('error', data["message"]);
						}
					}
				});
			},
			"取消": function() {
				$(this).dialog('close');
			}
		},
		contentText:"是否删除物流地址？",
	});
}

//设置默认地址
function DefaultAddress(addressType,express_address_id){
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/express/modifyshopexpressaddress')}",
		data : { "addressType":addressType, "express_address_id" : express_address_id },
		success : function(res) {
			if(res['code']>0){
				//showMessage('success',res['message']);
				LoadingInfo(1);
			}else{
				showMessage('error', res['message']);
			}
		}
	});
}

$(function(){
	//三级联动js
	$('[name="selProvince"]').html('<option value="-1">请选择省份</option>')
	$('[name="selCity"]').html('<option value="-1">请选择市</option>')
	$('[name="selArea"]').html('<option value="-1">请选择区/县</option>')
	
	/*省加载*/
	$.ajax({
		type:'post',
		url:"{:__URL('ADMIN_MAIN/express/getprovincelist')}",
		datatype:'json',
		success : function(data) {
			var optionhtml="";
			for(var i=0;i<data.length;i++){
				optionhtml+='<option value="'+data[i]['province_id']+'">'+data[i]['province_name']+'</option>';
			}
			$('[name="selProvince"]').append(optionhtml);
			if(isnull($('#cityid').val())){
				$('[name="selProvince"]').change();
			}
		}
	});	
	
	/*市加载*/
	$('[name="selProvince"]').change(function(){
		if($(this).val()!='-1'){
			$('[name="selCity"]').html('<option value="-1">请选择市</option>')
			$.ajax({
				type:'post',
				url:"{:__URL('ADMIN_MAIN/express/getcitylist')}",
				data:{'province':$(this).val()},
				datatype:'json',
				success : function(data) {
					var optionhtml="";
					for(var i=0;i<data.length;i++){
						optionhtml+='<option value="'+data[i]['city_id']+'">'+data[i]['city_name']+'</option>';
					}
					$('[name="selCity"]').append(optionhtml);
					var cityid=$('#cityid').val();
					if(cityid>0){
						$('[name="selCity"]').val(cityid);
					}
					if(isnull($('#districtid').val())){
						$('[name="selCity"]').change();
					}
				}
			});
		}
	})
	
	/*区/县加载*/
	$('[name="selCity"]').change(function(){
		if($(this).val()!='-1'){
			$('[name="selArea"]').html('<option value="-1">请选择区/县</option>')
			$.ajax({
				type:'post',
				url:"{:__URL('ADMIN_MAIN/express/getdistrictlist')}",
				data:{'city':$(this).val()},
				datatype:'json',
				success : function(data) {
					var optionhtml="";
					for(var i=0;i<data.length;i++){
						optionhtml+='<option value="'+data[i]['district_id']+'">'+data[i]['district_name']+'</option>';
					}
					$('[name="selArea"]').append(optionhtml);
					var districtid=$('#districtid').val();
					if(districtid>0){
						$('[name="selArea"]').val(districtid);
					}
				}
			});
		}
	})
	
	$('[aria-hidden="true"]').click(function(){
		$(this).parents('div[role="dialog"]').hide();
	});
	
});
	
//物流地址添加框显示
function addExpressAddressDialog(express_address_id){
	$('#express_address_id').val(express_address_id);
	if(express_address_id>0){
		$.ajax({
			type:'post',
			url:"{:__URL('ADMIN_MAIN/express/expressaddressinfo')}",
			data:{'express_address_id':express_address_id},
			success:function(data){
				$('#contact').val(data['contact']);
				$('#mobile').val(data['mobile']);
				$('#phone').val(data['phone']);
				$('#company_name').val(data['company_name']);
				$('#zipcode').val(data['zipcode']);
				$('#address').val(data['address']);
				$('#selProvince').val(data['province']);
				$('#cityid').val(data['city']);
				$('#districtid').val(data['district']);
				$('#selProvince').change();
			}
		})
		$("#addExpressAddressDialog").modal("show");
	}else{
		$('#addExpressAddressDialog input[type="text"]').val('');
		$('#addExpressAddressDialog select').val('-1');
		$('#addExpressAddressDialog select[name="selCity"]').html('<option value="-1">请选择市</option>');
		$('#addExpressAddressDialog select[name="selArea"]').html('<option value="-1">请选择区/县</option>');
		$("#addExpressAddressDialog").modal("show");
	}
}
/**
 * 错误提示
 * @param {Object} message 提示内容
 * @param {Object} promptid 提示id
 */
function Prompt(message,promptid){
	$('.input-error').hide();
	$(promptid).show().html('<span class="s">×</span>'+message);
}

 /**
 * 判断非空 为空返回 false
 * @param {Object} values
 */
function isnull(values){
	if(values=="" || values=="0" || values=="-1"){
		return false;
	}else{
		return true;
	}
}

 /**
 * 判断是否为手机号 真为true
 * @param {Object} values
 */
function ismobile(values){
	var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
	if(!myreg.test(values)){
		return false; 
	}else{
		return true;
	}
}

 /**
 * 物流地址添加
 */
function addExpressAddress(){
	var contact=$('#contact').val();
	var mobile=$('#mobile').val();
	var phone=$('#phone').val();
	var company_name=$('#company_name').val();
	var province=$('#selProvince').val();
	var city=$('#selCity').val();
	var district=$('#selArea').val();
	var zipcode=$('#zipcode').val();
	var address=$('#address').val();
	
	if(!isnull(contact)){
		Prompt("不可为空",'#contactPrompt');
		return false;
	}
	if(!isnull(district)){
		Prompt("不可为空",'#pcaPrompt');
		return false;
	}
	if(!isnull(address)){
		Prompt("不可为空",'#addressPrompt');
		return false;
	}
	if(!isnull(zipcode)){
		Prompt("不可为空",'#zipcodePrompt');
		return false;
	}
	if(!isnull(phone)){
		Prompt("不可为空",'#phonePrompt');
		return false;
	}
	if(!isnull(mobile)){
		Prompt("不可为空",'#mobilePrompt');
		return false;
	}
	if(!ismobile(mobile)){
		Prompt("手机号格式不正确",'#mobilePrompt');
		return false;
	}
	if(!isnull(company_name)){
		Prompt("不可为空",'#company_namePrompt');
		return false;
	}
	var express_address_id=$('#express_address_id').val();
	//如果为空或者0就是添加
	if(!isnull(express_address_id)){
		$.ajax({
			type:'post',
			url:"{:__URL('ADMIN_MAIN/express/addexpressaddress')}",
			data:{
				'contact':contact, 
				'mobile':mobile,
				'phone':phone, 
				'company_name':company_name,
				'province':province, 
				'city':city, 
				'district':district,
				'zipcode':zipcode,
				'address':address
			},
			datatype:'json',
			success : function(res) {
				$('#addExpressAddressDialog').modal("hide");
				if(res['code']>0){
					showMessage('success',res['message']);
					LoadingInfo(1);
				}else{
					showMessage('error', res['message']);
				}
			}
		});
	}else{
			$.ajax({
			type:'post',
			url:"{:__URL('ADMIN_MAIN/express/updateexpressaddress')}",
			data:{
				'express_address_id':express_address_id,
				'contact':contact, 
				'mobile':mobile,
				'phone':phone, 
				'company_name':company_name, 
				'province':province, 
				'city':city, 
				'district':district,
				'zipcode':zipcode,
				'address':address
			},
			datatype:'json',
			success : function(res) {
				$('#addExpressAddressDialog').modal("hide");
				if(res['code']>0){
					showMessage('success',res['message']);
					LoadingInfo(1);
				}else{
					showMessage('error', res['message']);
				}
			}
		});
	}
}
</script>
{/block}